package lab_2.windows;


import java.util.GregorianCalendar;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;


/**
 * Entity for Companion
 * @author leshukov
 */
public class Companion extends JScrollPane {

    public static enum Sender {
        ThisUser,
        OtherUser
    }

    
    public Companion( int idUser, String nickName ) {
        setUserID( idUser ) ;
        setUserName( nickName ) ;
        getViewport().add(getHistory()) ;
        setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER) ;
        getHistory().setEditable(false) ;
        getHistory().setContentType( "text/html" ) ;
    }


    /** Delivery message to this Companion (Drow in window) */
    public void deliverMessage(String msg, Sender sender) {
        GregorianCalendar date = new GregorianCalendar() ;

        int hours = date.get( GregorianCalendar.HOUR_OF_DAY ) ;
        int minutes = date.get( GregorianCalendar.MINUTE ) ;

        //Forming message
        StringBuilder builder = new StringBuilder() ;
        if( sender.equals( Sender.ThisUser )) {
            builder.append( "<font color=blue><font size=-2>" ) ;
        }
        else {
            builder.append( "<font color=green><font size=-2>" ) ;
        }
        builder.append( hours ) ;
        builder.append( ':' ) ;
        builder.append( minutes ) ;
        builder.append( "</font><strong> " ) ;
        if( sender.equals( Sender.ThisUser )) {
            builder.append( "Я" ) ;
        }
        else {
            builder.append( getUserName() ) ;
        }
        builder.append( "</strong></font> " ) ;
        builder.append( msg ) ;
        builder.append( "<br>" ) ;

        String oldMessage = getHistory().getText() ;
        //Trim header and tail from message
        final int LENGHT_OF_HEADER = 45 ;
        final int LENGTH_OF_TAIL = 19 ;
        oldMessage = oldMessage.substring( LENGHT_OF_HEADER, oldMessage.length() - LENGTH_OF_TAIL );

        String messageToAdd ;
        messageToAdd = oldMessage + builder.toString() ;

        getHistory().setText( messageToAdd ) ;
    }

    
    @Override
    public String toString() {
        int authorizedLength = 10 ;
        int lengthThreeDots = 3 ;
        if( getUserName().length() <= authorizedLength ) {
            while( (authorizedLength-getUserName().length()) != 0 )
                setUserName(getUserName()+" ") ;
            return getUserName() ;
        }
        else {
            String nick = getUserName().substring(0, authorizedLength-lengthThreeDots) ;
            return nick + "..." ;
        }
    }




    //-----Fields-----

    private int userID ;
    private String userName ;
    private JEditorPane  history = new JEditorPane() ;

    //----Getters and Setters

    public int getUserID() {
        return userID;
    }

    private void setUserID(int userID) {
        this.userID = userID;
    }

    private String getUserName() {
        return userName;
    }

    private void setUserName(String userName) {
        this.userName = userName;
    }

    private JEditorPane getHistory() {
        return history ;
    }
}
